package com.takeaway.ui.fragment

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.takeaway.R
import com.takeaway.model.beans.Order
import com.takeaway.presenter.OrderFragmentPresenter
import com.takeaway.ui.adapter.OrderRvAdapter
import kotlinx.android.synthetic.main.fragment_order.*
import org.jetbrains.anko.find

class OrderFragment : Fragment() {
    private lateinit var order_view: RecyclerView
    private lateinit var orderFragmentPresenter: OrderFragmentPresenter
    private lateinit var orderAdapter: OrderRvAdapter

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = View.inflate(activity, R.layout.fragment_order, null)
        order_view = view.find(R.id.rv_order_list)
        order_view.layoutManager = LinearLayoutManager(activity)
        orderFragmentPresenter = OrderFragmentPresenter(this)
        orderAdapter = OrderRvAdapter(activity)
        order_view.adapter = orderAdapter

        val srl_order = view.find<SwipeRefreshLayout>(R.id.srl_order)
        srl_order.setOnRefreshListener {
            Log.e("order", "refresh")
            // 再次请求数据
            orderFragmentPresenter.getOrderList("1")
        }
        return view
    }


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        // 请求数据
        orderFragmentPresenter.getOrderList("1")
    }

    fun OnSuccess(dataList: ArrayList<Order>) {
        orderAdapter.setData(dataList)
        // 关闭刷新
        srl_order.isRefreshing = false
    }


}